*Recoding ZA2965 

use "Data\ZA2965\ZA2965.dta" , clear

*Support for democracy
gen $idea_democracy				= V024	//"Was würden Sie, im Vergleich zu anderen Staatsideen, zur Idee der Demokratie sagen? Nehmen Sie dazu bitte die folgende Skala. Die Zahl 1 bedeudet, daß die Demokratie díe beste vorstellbare Staatsidee ist, die Zahl 6 beduetet, daß die Demokratie die schlechteste vorstellbare Staatsidee ist. Mit den Zahlen dazwischen können Sie ihre Meinung abstufen."

*define missing values
mvdecode $idea_democracy, mv(9=.a)  //.a = no answer

*reverse scale
replace $idea_democracy = (6-idea_democracy)/5 if !missing($idea_democracy)	 

*Support for liberal democracy
gen $libdem_opposition			= V025G
gen $libdem_freeopinion			= V025E
gen $libdem_partyopportunity	= V025H
gen $libdem_criticism			= V025F						
gen $libdem_conflict			= V025A							
gen $libdem_generalwill			= V025J		

*Vertrauen Institutionen
gen $trust_parties = V013P
gen $trust_bundestag = V013B
gen $trust_government = V013C
gen $trust_verfassungsgericht = V013A

*Efficacy
gen $politicians_takecare = V028B
gen $politicians_interestpublic = V028E
gen $ego_politicscomplicated = V028D


*define missing values
mvdecode $libdem_opposition $libdem_freeopinion $libdem_partyopportunity ///
	$libdem_criticism $libdem_conflict $libdem_generalwill ///
	$politicians_takecare $politicians_interestpublic $ego_politicscomplicated ///
	$trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht, ///
	mv(9=.a)  //.a = no answer
	
mvdecode $politicians_takecare $politicians_interestpublic $ego_politicscomplicated, mv(7 8=.a)

*Standardize range from 0 to 1; higher values indicate higher support for democracy
*reverse scale if necessary and standardize

foreach var of varlist $libdem_opposition $libdem_freeopinion $libdem_partyopportunity {
	replace `var'	= (6-`var')/5 if !missing(`var')
}

*standardize remaining items 

foreach var of varlist $libdem_criticism $libdem_conflict $libdem_generalwill {
	replace `var'	=(`var'-1)/5 if !missing(`var')
}

foreach var of varlist $trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht {
	replace `var' = (`var' - 1)/6 if !missing(`var')
}

foreach var of varlist $politicians_takecare $politicians_interestpublic {
	replace `var' = (`var' - 1)/4 if !missing(`var')
}

foreach var of varlist $ego_politicscomplicated  {
	replace `var' = (5 - `var')/4 if !missing(`var')
}

*Sociodemographic Controls
gen		$gender 			= S001-1

gen S002B_f					= S002B + 1900								//transform two-digit year of birth variable to four-digit variable

gen 	$age				= 1996-S002B_f							  	//survey year - year of birth	

gen 	$education			= S003
replace $education 			= 0 if $education 	<= 7 | $education==9 	// Still in School, Berufsschüler, none, Hautpschulabschl., Mittlere Reife, Polyt. Obersch., Fachhochschul. = 0
replace $education 			= 1 if $education 	== 8 					// Abitur/EOS = 1

gen $eastwest 				= split-1

*Weight
*Design Weight: wicht
gen weight = wichtges


*Party ID
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyid_category		= 1 	if inlist(S022, 1, 2, 4)
replace $partyid_category	= 0 	if inlist(S022, 3, 5, 6)
replace $partyid_category	= -1 	if inlist(S022, 7, 8)


*Party Vote
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyvote_category		= 1 	if inlist(S021, 1, 3)
replace $partyvote_category	= 0 	if inlist(S021, 2, 4, 5)
replace $partyvote_category	= -1 	if inlist(S021, 6, 7, 8, 9, 10, 11)

*Meta variables
gen $dataset 					= "ZA2965"
gen $year 						= 1996

*check
tab $idea_democracy V024, m
tab $libdem_opposition V025G, m
tab $libdem_freeopinion V025E, m
tab $libdem_partyopportunity V025H, m
tab $libdem_criticism V025F, m						
tab $libdem_conflict V025A, m					
tab $libdem_generalwill V025J, m		
tab $gender S001, m
tab $age S002B, m
tab $education S003, m
tab $eastwest split, m

*Save recode dataset
keep $idea_democracy $libdem_opposition	$libdem_freeopinion 					///
	$libdem_partyopportunity $libdem_criticism	$libdem_conflict 				///
	$politicians_takecare $politicians_interestpublic $ego_politicscomplicated ///
	$trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht ///
	$libdem_generalwill $gender $age $education $eastwest $dataset $year  weight	$partyid_category	$partyvote_category	
save ZA2965_recoded.dta, replace

